/*  
 * cafe-toolsbar.js
 * @Author :      GUORUI
 * @CreateDate :  2017年8月22日
 * Copyright (c) 2017, NRIET.　
 * 辅助工具条控件
 */
;
define(function () {
    //创建主界面
    function bulidHtml(cfg) {
        var $warp = $('<div>').addClass('cafe-busis-toolsbar');
        var $btn = $('<div>').addClass('toolsbar-btn');
        if (cfg.barList.menubar) {
            var $l = $("<div>").addClass('cafe-busis-toolsbar-left');
            var $u = $('<ul>');
            $.each(cfg.barList.menubar, function (i, o) {
                if (o.isHide) return;
                var $btn = $('<li>').attr("id", o.id).addClass(o.icon).text(o.name);
                $u.append($btn);
            })
            $l.append($u);
        }
        if (cfg.barList.toolsbar) {
            var $r = $("<div>").addClass('cafe-busis-toolsbar-right');
            var $u = $('<ul>');
            $.each(cfg.barList.toolsbar, function (i, o) {
                var $btn = $('<li>').attr("id", o.id).addClass(o.icon).text(o.name);
                if (o.isHide) $btn.hide();
                if (o.isDef) {
                    $btn.addClass("active");
                }
                switch (o.id) {
                    case "location":
                        var $g = $('<div>').addClass("cafe-at-location");
                        $.each([
                            { id: "csj", isDef: true, text: "长三角" },
                            { id: "china", isDef: false, text: "中国" },
                            { id: "dy", isDef: false, text: "东亚" }
                        ], function () {
                            var $gspan = $('<span>').attr("gid", this.id).text(this.text);
                            if (this.isDef) {
                                $gspan.addClass("active");
                            }
                            $g.append($gspan);
                        });
                        $btn.append($g.hide());
                        break;
                    case "station":
                        var $g = $('<div>').addClass("cafe-at-station");
                        $.each([
                            { id: "0", isDef: true, text: "基本站" },
                            { id: "1", isDef: false, text: "区域站" }
                        ], function () {
                            var $gspan = $('<span>').addClass("cas-station").attr("type", this.id).text(this.text);
                            if (this.isDef) {
                                $gspan.addClass("active");
                            }
                            $g.append($gspan);
                        });
                        $btn.append($g.hide());
                        break;
                    case "gistools":
                        var $g = $('<div>').addClass("cafe-at-gistools");
                        $.each([
                            { id: "enlarge", isDef: false, text: "放大" },
                            { id: "narrow", isDef: false, text: "缩小" },
                            { id: "distance", isDef: false, text: "测距" },
                            { id: "area", isDef: false, text: "测面积" },
                            { id: "changemap", isDef: false, text: "地图切换" },
                            { id: "zcd", isDef: false, text: "中尺度" }
                        ], function () {
                            var $gspan = $('<span>').addClass(this.id).attr("gid", this.id).text(this.text);
                            $g.append($gspan);
                        });
                        $btn.append($g.hide());
                        break;
                }
                $u.append($btn);
            })
            $r.append($u);
        }
        $warp.append($l).append($r).append($btn)
        return $warp;
    }
    /**
     * 按钮点击处理
     */
    function btnHandle($that, cfg) {
        switch ($that.attr("id")) {
            default:
				if ($.isFunction(cfg.call)) {
					cfg.call($that.attr("id"), $that.hasClass("active"));
				}
				break;
        }
    }
    /**
     * 绑定事件
     */
    function _eves($drop, cfg) {
        $drop.off().on("click", ".cafe-busis-toolsbar-left li", function () {
            if (!$(this).hasClass("disabled")) {
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
                } else {
                    $(this).addClass("active").siblings().removeClass('active');
                }
                btnHandle($(this), cfg);
            }
        }).on("click", ".cafe-busis-toolsbar-right li", function () {
            if (!$(this).hasClass("disabled")) {
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
                } else {
                    $(this).addClass("active");
                }
                btnHandle($(this), cfg);
            }
        }).on("click", ".cafe-at-gistools>span", function (e) { //gis工具
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            var type = $(this).attr("gid");
            switch (type) {
                case "enlarge": //放大
                    $(this).removeClass("active");
                    GIS.Extent.Zoom(cfg.gisId, true);
                    break;
                case "narrow": //缩小
                    $(this).removeClass("active");
                    GIS.Extent.Zoom(cfg.gisId, false);
                    break;
                case "distance"://距离
                    $(".area").removeClass('active');
                    GIS.Measure({
                        id: "at-gis",
                        type: "area",
                        status: "end",
                        GLMap: mapObj[cfg.gisId]
                    });
                    GIS.Measure({
                        id: "at-gis",
                        type: "distance",
                        status: $(this).hasClass("active") ? "start" : "end",
                        GLMap: mapObj[cfg.gisId]
                    });
                    break;
                case "area"://面积
                    $(".distance").removeClass('active');
                    GIS.Measure({
                        id: "at-gis",
                        type: "area",
                        status: "end",
                        GLMap: mapObj[cfg.gisId]
                    });
                    GIS.Measure({
                        id: "at-gis",
                        type: "area",
                        status: $(this).hasClass("active") ? "start" : "end",
                        GLMap: mapObj[cfg.gisId]
                    });
                    break;
                case "changemap"://切换地图
                    if ($(this).hasClass('active'))
                        GIS.Common.changeBaseLayer(cfg.gisId, 'online_WGS84', null);
                    else
                        GIS.Common.changeBaseLayer(cfg.gisId, 'quanguo_WGS84', null);
                    break;
                case "zcd":
                    $(this).attr('id', $(this).attr('gid'));
                    btnHandle($(this), cfg);
            }
        }).on("click", ".cas-station", function (e) { //站点选择
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            var type = $(this).attr("type");
            var isSel = $(this).hasClass("active");
            GIS.Station.eleShowControl(cfg.gisId, [type], ['WIND', 'VIS', 'TMP', 'TP', 'RH', 'SP'], isSel ? 'show' : 'hide');
        }).on("click", ".cafe-at-geography>span", function (e) { //地理信息
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            if ("demlayer" == $(this).attr("gid")) {
                GIS.LayerControll.showControll(cfg.gisId, "baselayer", !$(this).hasClass("active"));
            } else if ($(this).hasClass("active") && "bianjielayer" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer_noStr']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer_noStr", false);
            } else if ($(this).hasClass("active") && "bianjielayer_noStr" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer", false);
            }
            GIS.LayerControll.showControll(cfg.gisId, $(this).attr("gid"), $(this).hasClass("active"));
        }).on("click", '.toolsbar-btn', function (e) {
            $(this).toggleClass('active');
            if (!$(this).hasClass('active')) {
                $(".cafe-busis-toolsbar").removeClass('right0');
            } else {
                $(".cafe-busis-toolsbar").addClass('right0');
            }
        })
    }
    var CFToolsbar = function (settings) {
        var options = {
            appendTo: null, //控件所在容器
            gisId: null, //GIS的ID
            call: null //控件点击事件
        };
        this.config = $.extend({}, options, settings);

        var $container = $(this.config.appendTo);
        this.$drop = bulidHtml(this.config);
        _eves(this.$drop, this.config);

        if (this.config.appendTo) {
            $container.show().prepend(this.$drop);
        }
    }

    CFToolsbar.prototype = {
        disabledBtn: function (ids) {
            this.$drop.find("li").each(function () {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("active").addClass("disabled");
                }
            });
        },
        activeBtn: function (ids, isNoSelected) {
            this.$drop.find("li").each(function () {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("disabled");
                    if (!isNoSelected) $(this).addClass("active");
                }
            });
        },
        removeActive: function (ids) {
            this.$drop.find("li").each(function () {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("active");
                }
            });
        },
        getArea: function () {
            var $area = this.$drop.find(".cafe-at-zoneswitch>span.active");
            return {
                areaCode: $area.attr("areacode"),
                areaId: $area.attr("areaid")
            };
        },
        getUnActiveStationType: function () {
            var st = [];
            this.$drop.find(".cafe-at-station .cas-station").each(function () {
                if (!$(this).hasClass('active'))
                    st.push($(this).attr("type"));
            });
            return st;
        },
        getStationType: function () {
            var st = [];
            this.$drop.find(".cafe-at-station .cas-station.active").each(function () {
                st.push($(this).attr("type"));
            });
            return st.join(",");
        },
        setStationType: function (type) {
            this.$drop.find(".cafe-at-station .cas-station[type$='" + type + "']").show().siblings(".cas-station:not(span[type$='" + type + "'])").hide();
        },
        setStationSelFun: function (fun) {
            if (!this.config.stationSelFun) {
                this.config.stationSelFun = [];
            }
            this.config.stationSelFun.push(fun);
        },
        setAreaSelFun: function (fun) {
            this.config.areaSelFun = fun;
        },
        hideDemlayer: function () {
            var $demlayer = this.$drop.find(".cafe-at-geography>span[gid='demlayer']");
            if ($demlayer.hasClass("active")) {
                $demlayer.click();
            }
        },
        trigger: function (id) {
            this.$drop.find("#" + id).click();
        }
    };

    CafeJS.CFToolsbar = function (settings) {
        var drop = new CFToolsbar(settings);
        return drop;
    }
})